package com.travel.dao.impl;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.travel.base.impl.BaseDaoImpl;
import com.travel.bean.User;
import com.travel.dao.UserDao;

@Repository("userDaoImpl")
public class UserDaoImpl extends BaseDaoImpl<User> implements UserDao {

		/**
		 * 按照条件分页查询用户数据
		 */
	@Override
	public List<User> getUserByMap(Map map, Integer pageNum, Integer pageSize) {
		// TODO Auto-generated method stub
		Session session=this.hibernateTemplate.getSessionFactory().openSession();
		Criteria criteria=session.createCriteria(User.class);
		if(map!=null){
			criteria.add(Restrictions.allEq(map));
		}
		
		if(pageNum!=null&&pageSize!=null){
			criteria.setFirstResult((pageNum-1)*pageSize);
			criteria.setMaxResults(pageSize);
		}
		List<User> users=criteria.list();
		if(session!=null&&session.isOpen()){
			session.close();
		}
		
		return users;
	}

		@Override
		public Integer getUserCountByMap(Map map) {
			// TODO Auto-generated method stub
			Session session=this.hibernateTemplate.getSessionFactory().openSession();
			Criteria criteria=session.createCriteria(User.class);
			if(map!=null){
				criteria.add(Restrictions.allEq(map));
			}
			Long count=(Long)criteria.setProjection(Projections.rowCount()).uniqueResult();
			if(session!=null&&session.isOpen()){
				session.close();
			}
			return count.intValue();
		}


}
